package com.gzy.literature.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gzy.literature.dao.WorksDao;
import com.gzy.literature.entity.Users;
import com.gzy.literature.entity.Works;
import com.gzy.literature.service.WorksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageImpl;


import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service("worksService")
public class WorksServiceImpl implements WorksService {

    @Autowired
    private WorksDao worksDao; // 假设存在一个 WorksDao 类

    @Override
    public PageInfo<Works> queryBll(Works works, int pageNum, int pageSize) {
        if (pageNum <= 0 || pageSize <= 0) {
            throw new IllegalArgumentException("Page number and page size must be positive integers");
        }
        PageHelper.startPage(pageNum, pageSize); // 假设 PageHelper 是一个分页工具类
        List<Works> worksList = worksDao.queryBll(works); // 假设 queryAll 方法接受 Works 对象作为筛选条件
        return new PageInfo<>(worksList);
    }

    @Override
    public Works queryById(Integer id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("Work ID must be a positive integer");
        }
        return worksDao.queryById(id);
    }

    @Override
    public Works insert(Works works) {
        if (works == null || works.getId() == null) { // 替换 SomeRequiredField 为 Works 类中的实际必填字段
            throw new IllegalArgumentException("Works object and some required field must not be null");
        }
        try {
            worksDao.insert(works);
        } catch (Exception e) {
            // 记录日志或执行其他错误处理逻辑
            throw new RuntimeException("Failed to insert works", e);
        }
        return works;
    }

    @Override
    public Works update(Works works) {
        if (works == null || works.getId() == null || works.getId() <= 0 || works.getId() == null) {
            // 替换 SomeRequiredField 为 Works 类中的实际必填字段
            throw new IllegalArgumentException("Works object, ID, and some required field must be valid");
        }
        try {
            worksDao.update(works);
        } catch (Exception e) {
            // 记录日志或执行其他错误处理逻辑
            throw new RuntimeException("Failed to update works", e);
        }
        // 假设更新后需要重新查询以获取最新数据
        return queryById(works.getId());
    }

    @Override
    public boolean deleteById(Integer id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("Work ID must be a positive integer");
        }
        try {
            int affectedRows = worksDao.deleteById(id);
            return affectedRows > 0;
        } catch (Exception e) {
            // 记录日志或执行其他错误处理逻辑
            throw new RuntimeException("Failed to delete works", e);
        }
    }
}